package com.derbysoft.synxisadapter.dswitch3.translator;

import com.derbysoft.dswitch.dto.hotel.reservation.CancelInfoDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelCancelRS;
import com.derbysoft.dswitch.remote.hotel.dto.ErrorDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelCancelResponse;
import com.derbysoft.synxisadapter.commons.dto.synxis.OTACancelRQ;
import com.derbysoft.synxisadapter.commons.dto.synxis.OTACancelRS;
import com.derbysoft.synxisadapter.dswitch2.translator.Translator;
import com.derbysoft.synxisadapter.webservice.impl.ResponsePair;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * User: jason
 * Date: 2012-11-29
 */
@Component("ds3CancelRSTranslator")
public class HotelCancelRSTranslator extends AbstractTranslator
        implements Translator<List<ResponsePair<OTACancelRQ, OTACancelRS>>, HotelCancelResponse, String> {

    private static final String SEPARATOR = ";";

    @Override
    public HotelCancelResponse translate(List<ResponsePair<OTACancelRQ, OTACancelRS>> responsePairs, String taskId) {
        if (failedResponses(responsePairs)) {
            return createFailedResponse(responsePairs, taskId);
        }
        HotelCancelResponse response = new HotelCancelResponse();
        response.setTaskId(taskId);
        HotelCancelRS hotelCancelRS = new HotelCancelRS();
        hotelCancelRS.setCancelInfo(createCancelInfo(responsePairs));
        response.setHotelCancelRS(hotelCancelRS);
        return response;
    }

    private CancelInfoDTO createCancelInfo(List<ResponsePair<OTACancelRQ, OTACancelRS>> responsePairs) {
        CancelInfoDTO cancelInfoDTO = new CancelInfoDTO();
        StringBuilder builder = new StringBuilder();
        for (ResponsePair<OTACancelRQ, OTACancelRS> responsePair : responsePairs) {
            builder.append(responsePair.getResponse().getUniqueID().get(0).getID()).append(SEPARATOR);
        }
        cancelInfoDTO.setHotelCancellationNO(StringUtils.removeEnd(builder.toString(), SEPARATOR));
        return cancelInfoDTO;
    }

    private HotelCancelResponse createFailedResponse(List<ResponsePair<OTACancelRQ, OTACancelRS>> responsePairs, String taskId) {
        HotelCancelResponse response = new HotelCancelResponse();
        response.setTaskId(taskId);
        ErrorDTO errorDTO = new ErrorDTO();
        errorDTO.setCode("CancelFailed");
        errorDTO.setSource(AbstractTranslator.PROVIDER_SOURCE);
        errorDTO.setMessage(getErrorMsg(responsePairs));
        response.setError(errorDTO);
        return response;
    }

    private String getErrorMsg(List<ResponsePair<OTACancelRQ, OTACancelRS>> responsePairs) {
        StringBuilder builder = new StringBuilder();
        for (ResponsePair<OTACancelRQ, OTACancelRS> responsePair : responsePairs) {
            if (responsePair.getResponse().getErrors() != null) {
                String confirmId = responsePair.getRequest().getUniqueID().get(0).getID();
                String shortText = responsePair.getResponse().getErrors().getError().get(0).getShortText();
                builder.append(String.format("ConfirmNumber :[%s], ErrorMessage:[%s]", confirmId, shortText));
                builder.append(";");
            }
        }
        return builder.toString();
    }

    private boolean failedResponses(List<ResponsePair<OTACancelRQ, OTACancelRS>> responsePairs) {
        for (ResponsePair<OTACancelRQ, OTACancelRS> responsePair : responsePairs) {
            if (responsePair.getResponse().getErrors() != null) {
                return true;
            }
        }
        return false;
    }
}
